/*
 * direccionamiento.c
 *
 *  Created on: 02/10/2011
 *      Author: utn_so
 */

#include "direccionamiento.h"
#include "structsFAT.h"
#include <stdlib.h>
#include <stdint.h>

void FIFO_SECTOR_Push(FIFO_SECTOR * aSectorList, SECTOR aSector)
{
	Nodo_SECTOR * aux = (Nodo_SECTOR *) malloc(sizeof(Nodo_SECTOR));

	aux->sector = aSector;
	aux->nxt = NULL;

	if (aSectorList->Head == NULL) {
		aSectorList->Head = aux;
		aSectorList->Tail = aux;
	}
	else {
		aSectorList->Tail->nxt = aux;
		aSectorList->Tail = aux;
	}
}

void mapClusterToSectors(CLUSTER aCluster, BootSector BootSectorInfo, FIFO_SECTOR * outListSectors)
{
	SECTOR firstSector;
	int32_t i;

	firstSector = BootSectorInfo.reserved_sectors +
				  (BootSectorInfo.number_of_FATs * BootSectorInfo.Sectors_per_fileAllocationTable) +
				  (aCluster - 2) * BootSectorInfo.sectors_per_cluster;

	for (i = 0; i < BootSectorInfo.sectors_per_cluster - 1; i++)
		FIFO_SECTOR_Push(outListSectors, firstSector + i);
}

void mapBloqueToSectors(BLOQUE aBloque, FIFO_SECTOR * outListSectors)
{
	int32_t i;
	for (i = 0; i < sectoresPorBloque; i++)
		FIFO_SECTOR_Push(outListSectors, (aBloque * sectoresPorBloque) + i);
}

void FIFO_SECTOR_Destroy(FIFO_SECTOR * aSectorList)
{
	Nodo_SECTOR * aux;
	while (aSectorList->Head != NULL) {
		aux = aSectorList->Head;
		aSectorList->Head = aSectorList->Head->nxt;
		free(aux);
	}
	aSectorList->Head = NULL;
	aSectorList->Tail = NULL;
}

void FIFO_SECTOR_Create(FIFO_SECTOR * aSectorList)
{
	aSectorList->Head = NULL;
	aSectorList->Tail = NULL;
}
